/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.dialogs;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.googlecode.umlsynch.eclipse.rpc.Debug.TEclipseDebug;

/**
 * This class demonstrates how to create your own dialog classes. It allows users
 * to input a String
 */
public class FilesSelectDialog extends Dialog {

	public String[] fileList;
	public String[] nameList;

	public String[] SelectedFileList;
	public String[] SelectedNameList;
	

  
    private TEclipseDebug debug = new TEclipseDebug("FilesSelectDialog");


  /**
   * InputDialog constructor
   * 
   * @param parent the parent
   */
  public FilesSelectDialog(Shell parent) {
    // Pass the default styles here
    this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
  }

  /**
   * InputDialog constructor
   * 
   * @param parent the parent
   * @param style the style
   */
  public FilesSelectDialog(Shell parent, int style) {
    // Let users override the default styles
    super(parent, style);
    debug.Note("FilesSelectDialog");
    setText("Please choose the File:");    
  }

  /**
   * Gets the message
   * 
   * @return String
   */
  public String[] getSelectedNameList() {
    
	return this.SelectedNameList;
  }

  /**
   * Gets the message
   * 
   * @return String
   */
  public String[] getSelectedFileList() {
    
	return this.SelectedFileList;
  }
  
  /**
   * Sets the message
   * 
   * @param message the new message
   */
  public void setFileList(String[] flist) {
    fileList = flist.clone();
  }

  /**
   * Sets the message
   * 
   * @param message the new message
   */
  public void setNameList(String[] flist) {
    nameList = flist.clone();
  }

  /**
   * Opens the dialog and returns the input
   * 
   * @return String
   */
  public void open() {
	  
	  // Create the dialog window
      Shell shell = new Shell(getParent(), getStyle());
      
      createContents(shell);
      
      shell.pack();
    
      shell.open();

    
      Display display = getParent().getDisplay();

      while (!shell.isDisposed()) {
    	  if (!display.readAndDispatch()) {
    		  display.sleep();
    	  }
      }

  }

  /**
   * Creates the dialog's contents
   * 
   * @param shell the dialog window
   */
  private void createContents(final Shell shell) {
	
    shell.setLayout(new GridLayout(1, true));
    shell.setText(getText());
        
    // Show the message
    Label label = new Label(shell, SWT.NONE);
    label.setText("Please select class:");
    GridData lgrid = new GridData(400, 20);
    lgrid.grabExcessHorizontalSpace = true;
    lgrid.horizontalAlignment = SWT.FILL;
    label.setLayoutData(lgrid);
    label.pack();
    
    final Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.CHECK);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    TableColumn column1 = new TableColumn(table, SWT.NONE );
    column1.setText("");
    
    TableColumn column2 = new TableColumn(table, SWT.NONE );
    column2.setText("class name");
    
    TableColumn column3 = new TableColumn(table, SWT.NONE );
    column3.setText("path");

    if (fileList != null && (fileList.length > 0))
    {
    	for (int i = 0; i < fileList.length; i++) {
    		TableItem item = new TableItem(table, SWT.NONE );
    		item.setText(0, "");
    		item.setText(1, nameList[i]);
    		item.setText(2, fileList[i]);
    	}

    	for (int i=0; i<3; i++) {
    		table.getColumn (i).setMoveable(true);
    		table.getColumn (i).setResizable(true);
    		table.getColumn (i).pack();
    	}     
    }  

    //table.setSize(200,400);
    int height =  table.getHeaderHeight() + (fileList.length - 1) * table.getItemHeight();
    if (height > 500)
    	height = 500;
    GridData grid = new GridData(400,height);
    grid.grabExcessHorizontalSpace = true;
    grid.horizontalAlignment = SWT.FILL;
    table.setLayoutData(grid);
    table.pack();
    
    Composite compositeButtons = new Composite(shell, SWT.NO_FOCUS);

    GridData cgrid = new GridData(400, 40);
    cgrid.grabExcessHorizontalSpace = true;
    cgrid.horizontalAlignment = SWT.FILL;
    compositeButtons.setLayoutData(cgrid);
    compositeButtons.setLayout(new RowLayout(4));

    Button ok = new Button(compositeButtons , SWT.PUSH);
    ok.setText("OK");
    
    ok.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        
    	int checkCount = 0;
    	for (int i=0; i<table.getItemCount(); i++)
    		if (table.getItems()[i].getChecked())
    			checkCount++;

    	if (checkCount > 0)
    	{
    		SelectedFileList = new String[checkCount];
    		SelectedNameList = new String[checkCount];
    		checkCount = 0;

    		for (int i=0; i<table.getItemCount(); i++)
    			if (table.getItems()[i].getChecked()) {
    				SelectedFileList[checkCount] = fileList[i];
    				SelectedNameList[checkCount] = nameList[i];
    				checkCount++;
    			}
    	}	   
    	
        shell.close();
      }
    });

    Button cancel = new Button(compositeButtons , SWT.PUSH);
    cancel.setText("Cancel");

    cancel.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
    	  SelectedFileList = null;
    	  SelectedNameList = null;
        shell.close();
      }
    });

    Button deselectAll = new Button(compositeButtons , SWT.PUSH);
    deselectAll.setText("Deselect all");
    deselectAll.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
    	for (int i=0; i<table.getItemCount(); i++)
    		table.getItems()[i].setChecked(false);
      }
    });

    Button selectAll = new Button(compositeButtons , SWT.PUSH);
    selectAll.setText("Select All");
    selectAll.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
      	for (int i=0; i<table.getItemCount(); i++)
    		table.getItems()[i].setChecked(true);
      }
    });

    // Set the OK button as the default, so
    // user can type input and press Enter
    // to dismiss
    compositeButtons.pack();
    shell.setDefaultButton(ok);
  }
}
